<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/7
 * Time: 15:43
 */

namespace app\home\controller;


use app\common\controller\HomeBase;
use think\Db;
use think\Request;

class Article extends HomeBase
{
    public function index()
    {
        $id                = Request::instance()->param('id');
        //文章信息
        $article           = Db::name('article')
            ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, attr, tpl, create_time, update_time')
            ->where('id', $id)
            ->find();
        if (empty($article))
        {
            return $this->fetch('/public/404');
        }
        //栏目信息
        $category          = Db::name('category')
            ->field('id, title, other_name, thumb, seo_title, seo_keywords, seo_desc, list_tpl, article_tpl, attr')
            ->where('id', $article['category_id'])
            ->find();
        $title             = $this->title;
        $description       = $this->description;
        $keywords          = $this->keywords;
        //标题
        if (!empty($article['title']))
        {
            $title = $article['title'].' - '.$title;
        } else {
            if (!empty($category['seo_title']))
            {
                $title = $category['seo_title'];
            }
        }
        //关键字
        if (!empty($article['keywords']))
        {
            $title = $article['keywords'];
        } else {
            if (!empty($category['seo_keywords']))
            {
                $keywords = $category['seo_keywords'];
            }
        }
        //描述
        if (!empty($article['description']))
        {
            $title = $article['description'];
        } else {
            if (!empty($category['seo_desc']))
            {
                $description = $category['seo_desc'];
            }
        }
        //模版文件
        if (!empty($article['tpl']))
        {
            $tpl = $article['tpl'];
        } else {
            $tpl = $category['article_tpl'];
        }
        return $this->fetch("/$tpl", [
            'category'    => $category,
            'article'     => $article,
            'title'       => $title,
            'keywords'    => $keywords,
            'description' => $description,
        ]);
    }

    //空操作
    public function _empty($name)
    {
        return view('/public/404');
    }
}